package com.leet.code.lcof;

public class Code44 {

    public static void main(String[] args) {
//        int sum = 0;
//        for (int i = 0; i <= 999; i++) {
//            sum++;
//        }
//        System.out.println(sum);

        System.out.println(new Code44().findNthDigit(1000000000));
    }


    public int findNthDigit(int n) {

        if (n == 0) {
            return 0;
        }

        int i = 0;
        long l = 0L;
        long lastL = 0;

        while (l < n) {
            lastL = l;
            i++;
            l = l + i * pow10(9, i - 1);
        }

        long p = n - lastL;

        long y = p / i;
        long d = p % i;

        y = y + pow10(1, i - 1) - 1;
        if (d == 0) {
            d = i - 1;
        } else {
            y = y + 1;
            d = d - 1;
        }

        char a = Long.toString(y).charAt((int)d);
        return Integer.parseInt(String.valueOf(a));

    }


    public static long pow10(long i, long p) {
        StringBuilder sb = new StringBuilder(String.valueOf(i));
        while (p > 0) {
            sb.append('0');
            p--;
        }
        return Long.parseLong(sb.toString());

    }


}
